package leetcode101.ch3;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

//LC 340 至多包含 K 个不同字符的最长子串
public class LengthOfLongestSubstringKDistinct2 {
    public int lengthOfLongestSubstringKDistinct(String s, int k) {
        if (s.length() <= k) {
            return s.length();
        }
        int[] map = new int[128];
        int count = 0;
        int l = 0, r= 0;
        for (; r < s.length();) {
            if (map[s.charAt(r++)]++ == 0) {
                count++;
            }
            if (count > k) {
                if (--map[s.charAt(l++)] == 0) {
                    count--;
                }
            }
        }
        return r - l;
    }

    public static void main(String[] args) {
        new LengthOfLongestSubstringKDistinct2().lengthOfLongestSubstringKDistinct("abee", 1);
    }
}
